using Gtk;

namespace Zoo
{

public class Target
{
    public string name { get; set; }
    public string directory { get; set; }
    public string compiler { get; set; }
    public string architecture { get; set; }
    public string transparency { get; set; }
    public string config_file { get; set; }

    public void show()
    {
        stdout.printf("name: %s\ndirectory: %s\ncompiler: %s\n",
                      name, directory, compiler);
        stdout.printf("architecture: %s\ntransparency: %s\nconfig_file: %s\n",
                      architecture, transparency, config_file);
    }
}

public class Scratchbox : GLib.Object
{
    private string base_dir;
    private List<Target> targets;
    private Gee.HashMap<string, Target> m_targets;

    private string _deboostrap_name;
    public string debootstrap_name { get; set; }

    public bool uses_debootstrap {
        get { return _deboostrap_name != ""; }
    }

    public signal void scratchbox_dir_changed();

    public Scratchbox(string base_dir = "")
    {
        this.base_dir = base_dir;
        parse_targets();
    }

    public List<string> get_target_names()
    {
        var t_names = new List<string>();
        foreach (Target target in this.targets) {
            t_names.append(target.name);
        }
        return t_names;
    }

    public string scratchbox_path()
    {
        return Path.build_filename(this.base_dir, "scratchbox");
    }

    public string user_path()
    {
        return Path.build_filename(scratchbox_path(), "users",
                                   Environment.get_user_name());
    }

    public string targets_path()
    {
        return Path.build_filename(user_path(), "targets");
    }

    public string transparency_path()
    {
        return Path.build_filename(scratchbox_path(), "devkits", "qemu", "bin");
    }

    private void parse_targets()
    {
        try {
            var config_regex = new Regex(".*\\.config");
            var dir = File.new_for_path(targets_path());
            var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);

            FileInfo file_info;
            while ((file_info = enumerator.next_file(null)) != null) {
                string config_file = file_info.get_name();
                if (config_regex.match(config_file))
                    read_config_file(config_file);
            }
        } catch (Error e) {
            stderr.printf("Error: %s\n", e.message);
        }
    }

    private void read_config_file(string filename)
    {
        var file = File.new_for_path(Path.build_filename(targets_path(), filename));
        Target target = new Target();
        target.config_file = filename;

        try {
            string line;
            var in_stream = new DataInputStream(file.read(null));

            while ((line = in_stream.read_line(null, null)) != null) {
                string[] pair = line.split("=", 2);

                switch (pair[0]) {
                case "SBOX_TARGET_NAME":
                    target.name = pair[1];
                    break;
                case "SBOX_TARGET_DIR":
                    target.directory = pair[1];
                    break;
                case "SBOX_CROSS_GCC_NAME":
                    target.compiler = pair[1];
                    break;
                case "SBOX_CPU":
                    target.architecture = pair[1];
                    break;
                case "SBOX_CPUTRANSPARENCY_METHOD":
                    target.transparency = pair[1];
                    break;
                }
            }
        } catch (Error e) {
            error("%s", e.message);
        }

        target.show();
        this.targets.append(target);
        //this.m_targets[target.name] = target;
    }

    public void change_scratchbox_basedir(string base_dir)
    {
        this.base_dir = base_dir;
        parse_targets();
        scratchbox_dir_changed();
    }

    public string current_target_name()
    {
        string current;

        string command = "scratchbox sb-conf current";
        if (uses_debootstrap)
            command = "schroot -d /tmp -c " + debootstrap_name + " " + command;

        try {
            Process.spawn_command_line_sync(command, out current);
        } catch (SpawnError e) {
            warning("Failed to run scratchbox command: %s\n", e.message);
        }

        return current.chomp();
    }

    //public Target current_target()
    //{
    //   return m_targets[current_target_name()];
    //}
}

} // namespace Zoo

